package com.tedu.weibo0521.controller;

import com.tedu.weibo0521.base.response.JsonResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping
public class fileController {
    public JsonResult upload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
        UUID uuid = UUID.randomUUID();
        filename= uuid + suffix;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd");
        String datePath = simpleDateFormat.format(new Date());
        String dirPath = "d:files/" ;
        File file1 = new File(dirPath + datePath);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        file.transferTo(new File(dirPath + datePath + filename));
        return JsonResult.success(datePath+filename);
    }

}
